// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CMIObjectives.java

package com.iphaeton.lms.course.scorm12.datamodel.cmi;

import com.iphaeton.lms.course.scorm12.DebugIndicator;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.Vector;

// Referenced classes of package com.iphaeton.lms.course.scorm12.datamodel.cmi:
//            CMICategory, CMIRequest, CMIObjectiveData, DMErrorManager

public class CMIObjectives extends CMICategory
    implements Serializable
{

    public CMIObjectives()
    {
        super(true);
        objectives = new Vector();
    }

    public Vector getObjectives()
    {
        return objectives;
    }

    public void setObjectives(CMIObjectiveData objective, int index)
    {
        objectives.add(index, objective);
    }

    public String performGet(CMIRequest theRequest, DMErrorManager dmErrorMgr)
    {
        String result = new String("");
        if(isValidObjRequest(theRequest))
        {
            String token = theRequest.getNextToken();
            if(theRequest.hasMoreTokensToProcess())
                try
                {
                    Integer tmpInt = new Integer(token);
                    int indexOfArr = tmpInt.intValue();
                    try
                    {
                        CMIObjectiveData objData = (CMIObjectiveData)objectives.elementAt(indexOfArr);
                        result = objData.performGet(theRequest, dmErrorMgr);
                    }
                    catch(ArrayIndexOutOfBoundsException e)
                    {
                        if(_Debug)
                        {
                            System.out.println("Element does not exist at the given index");
                            System.out.println("Index: " + indexOfArr);
                        }
                        dmErrorMgr.SetCurrentErrorCode("201");
                    }
                }
                catch(NumberFormatException nfe)
                {
                    if(_Debug)
                    {
                        System.out.println("Error - Data Model Element not implemented");
                        System.out.println("Invalid data model element: " + theRequest.getRequest() + " passed to LMSGetValue()");
                        System.out.println("Array index required");
                    }
                    dmErrorMgr.SetCurrentErrorCode("401");
                }
            else
            if(theRequest.isAChildrenRequest())
                result = getChildren();
            else
            if(theRequest.isACountRequest())
            {
                int count = 0;
                count = objectives.size();
                Integer tmpInt = new Integer(count);
                result = tmpInt.toString();
            } else
            {
                if(_Debug)
                {
                    System.out.println("Error - Data Model Element not implemented");
                    System.out.println("Invalid request: " + theRequest.getRequest());
                }
                dmErrorMgr.recNotImplementedError(theRequest);
            }
        } else
        {
            if(_Debug)
            {
                System.out.println("Error - Data Model Element not implemented");
                System.out.println("Invalid request: " + theRequest.getRequest());
            }
            dmErrorMgr.SetCurrentErrorCode("401");
        }
        theRequest.done();
        if(_Debug)
        {
            System.out.println("Returning from CMIObjectives::performGet()");
            System.out.println("Value returned: " + result);
        }
        return result;
    }

    public String getChildren()
    {
        String children = "id,score,status";
        return children;
    }

    public void performSet(CMIRequest theRequest, DMErrorManager dmErrorMgr)
    {
        int index = -1;
        String token = theRequest.getNextToken();
        try
        {
            Integer tmpInt = new Integer(token);
            index = tmpInt.intValue();
            CMIObjectiveData tmpObj = (CMIObjectiveData)objectives.elementAt(index);
            tmpObj.performSet(theRequest, dmErrorMgr);
            objectives.set(index, tmpObj);
        }
        catch(NumberFormatException nfe)
        {
            if(theRequest.isAKeywordRequest())
            {
                dmErrorMgr.recKeyWordError(token);
            } else
            {
                if(_Debug)
                {
                    System.out.println("Error - Data Model Element not implemented");
                    System.out.println("Invalid data model element: " + theRequest.getRequest() + " passed to LMSSetValue()");
                }
                dmErrorMgr.recNotImplementedError(theRequest);
            }
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            if(index <= objectives.size())
            {
                CMIObjectiveData objData = new CMIObjectiveData();
                objData.performSet(theRequest, dmErrorMgr);
                objectives.add(index, objData);
            } else
            {
                dmErrorMgr.SetCurrentErrorCode("201");
            }
        }
        theRequest.done();
    }

    private boolean isValidObjRequest(CMIRequest theRequest)
    {
        boolean rtrnFlag = false;
        if(theRequest.getTotalNumTokens() >= CMIOBJ_MIN_NUM_TOKENS)
            rtrnFlag = true;
        return rtrnFlag;
    }

    private static final long serialVersionUID = 0x2b2610db2f8c29d5L;
    private static boolean _Debug;
    public Vector objectives;
    static int CMIOBJ_MIN_NUM_TOKENS = 3;

    static 
    {
        _Debug = DebugIndicator.ON;
    }
}
